package com.chinasofti.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.chinasofti.common.IdWorker;
import com.chinasofti.dao.DeptRepository;
import com.chinasofti.dao.EmpRepository;
import com.chinasofti.dao.PerformanceRepository;
import com.chinasofti.manager.service.AdminEmpService;
import com.chinasofti.model.Dept;
import com.chinasofti.model.Emp;
import com.chinasofti.model.Performance;
import com.tramy.utils.DateUtils;
import com.tramy.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Creator: <br>
 * Date: <br>
 * Description: <br>
 */
@Service
@Component
public class AdminEmpServiceImpl implements AdminEmpService{
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private PerformanceRepository performanceRepository;
    @Autowired
    private EmpRepository empRepository;
    @Autowired
    private DeptRepository deptRepository;

    @Override
    public boolean addEmp(String name, String password,
                      String gender, Integer age,
                      String phone, String deptName,
                      Double salary, String address) {
        Emp emp = new Emp();
        emp.setId(idWorker.nextId());
        emp.setName(name);
        emp.setPassword(password);
        emp.setGender(gender);
        emp.setAge(age);
        emp.setPhone(phone);
        emp.setDeptName(deptName);
        emp.setSalary(salary);
        emp.setAddress(address);
        empRepository.save(emp);
        Performance performance = new Performance();
        performance.setId(idWorker.nextId());
        performance.setName(emp.getName());
        performance.setDeptName(emp.getDeptName());
        String month = DateUtils.dateToString(DateUtils.now(), "yyyy-MM");
        performance.setMonth(month);
        performance.setTotalPoints(30.00);
        performanceRepository.save(performance);
        return true;
    }

    @Override
    public List<Emp> queryEmpList() {
        return empRepository.findAll();
    }

    @Override
    public boolean updateEmp(String name, String password,
                             String gender, Integer age,
                             String phone, Double salary,
                             String address, Long id) {
        Emp emp = empRepository.findByIdEquals(id);
        if (!StringUtils.isEmpty(name)){
            emp.setName(name);
        }
        if (!StringUtils.isEmpty(password)){
            emp.setPassword(password);
        }
        if (!gender.equals("未选择")){
            emp.setGender(gender);
        }
        if (age != null){
            emp.setAge(age);
        }
        if (!StringUtils.isEmpty(phone)){
            emp.setPhone(phone);
        }
        if (salary != null){
            emp.setSalary(salary);
        }
        if (!StringUtils.isEmpty(address)){
            emp.setAddress(address);
        }
        empRepository.save(emp);
        return true;
    }

    @Override
    public void deleteEmp(Long id) {
        empRepository.deleteById(id);
    }

    @Override
    public List<Dept> queryDeptList() {
        return deptRepository.findAll();
    }

}
